class Solution1 {
    int[] tmp;
    public int reversePairs(int[] record) {
        tmp = new int[record.length];
        return merge(record, 0, record.length - 1);
    }
    public int merge(int[] record, int left, int right) {
        if (left >= right) return 0;
        int ret = 0;
        int mid = left + (right - left) / 2;

        ret += merge(record, left, mid);
        ret += merge(record, mid + 1, right);

        int cur1 = left, cur2 = mid + 1, i = 0;
        while (cur1 <= mid && cur2 <= right) {
            if (record[cur1] <= record[cur2]) {
                tmp[i++] = record[cur1++];
            } else {
                ret += mid - cur1 + 1;
                tmp[i++] = record[cur2++];
            }
        }
        while (cur1 <= mid) {
            tmp[i++] = record[cur1++];
        }
        while (cur2 <= right) {
            tmp[i++] = record[cur2++];
        }
        for (int j = left; j <= right; j++) {
            record[j] = tmp[j - left];
        }
        return ret;
    }
}